package org.ushio.nchu_22201404.dao;

import org.apache.ibatis.annotations.*;
import org.ushio.common_module.entity.ComplaintRecord;

import java.util.List;

@Mapper
public interface ComplaintRecordDao {

    @Insert("INSERT INTO complaint_record (Issue_Type, Issue_Details, Issue_Resolved, Phone_Number, Receipt_Status, Rating, Customer_Service_ID)" +
            "VALUES (#{issueType}, #{issueDetails}, 0, #{phoneNumber}, 0, 0, 1)")
    @Options(useGeneratedKeys = true, keyProperty = "complaintRecordID")
    void insertComplaintRecord(ComplaintRecord complaintRecord);

    @Update("UPDATE complaint_record " +
            "SET Issue_Resolved = TRUE, Rating = #{rating} " +
            "WHERE Phone_Number = #{phoneNumber}")
    void complaintSetIssueResolved(ComplaintRecord complaintRecord);

    @Select("SELECT Rating, Issue_Resolved " +
            "FROM complaint_record " +
            "WHERE Phone_Number = #{phoneNumber}")
    @Results({
            @Result(property = "rating", column = "Rating"),
            @Result(property = "issueResolved", column = "Issue_Resolved")
    })
    ComplaintRecord complaintGetIssueResolved(@Param("phoneNumber") String phoneNumber);

    @Update("UPDATE complaint_record " +
            "SET Receipt_Status = 1 " +
            "WHERE Phone_Number = #{phoneNumber}")
    void complaintSetIsSigned(ComplaintRecord complaintRecord);

    @Select("SELECT Receipt_Status " +
            "FROM complaint_record " +
            "WHERE Phone_Number = #{phoneNumber}")
    @Results({
            @Result(property = "receiptStatus", column = "Receipt_Status")
    })
    ComplaintRecord complaintGetIsSigned(@Param("phoneNumber") String phoneNumber);

    @Select("SELECT Issue_Type, Issue_Details, Phone_Number " +
            "FROM complaint_record\n" +
            "WHERE Receipt_Status = 0")
    @Results({
            @Result(property = "issueType", column = "Issue_Type"),
            @Result(property = "issueDetails", column = "Issue_Details"),
            @Result(property = "phoneNumber", column = "Phone_Number"),
            @Result(property = "ReceiptStatus", column = "Receipt_Status")
    })
    List<ComplaintRecord> selectComplaintRecordToCustomerService();

    @Select("SELECT * " +
            "FROM complaint_record")
    @Results({
            @Result(property = "complaintRecordID", column = "Complaint_Record_ID"),
            @Result(property = "issueType", column = "Issue_Type"),
            @Result(property = "issueDetails", column = "Issue_Details"),
            @Result(property = "issueResolved", column = "Issue_Resolved"),
            @Result(property = "complaintTime", column = "Complaint_Time"),
            @Result(property = "rating", column = "Rating"),
            @Result(property = "receiptStatus", column = "Receipt_Status"),
            @Result(property = "customerServiceID", column = "Customer_Service_ID"),
            @Result(property = "phoneNumber", column = "Phone_Number")
    })
    List<ComplaintRecord> selectComplaintRecordToAdmin();
}
